local extension=Package("xiaobai-ten")
extension.extensionName="xiaobai"

Fk:loadTranslationTable{
    ["xiaobai-ten"]="第十届小白杯",
    ["xiaobai"]="小白杯",
    
}

local U = require "packages/utility/utility"
local RUtil = require "packages/xiaobai/utility/rfenghou_util"

local function AddWinAudio(general)
  local Win = fk.CreateActiveSkill{ name = general.name.."_win_audio" }
  Win.package = extension
  Fk:addSkill(Win)
end

local liuying= General(extension, "xiaobai-ten__liuying", "shu", 4)--柳隐

--偷偷加强一下
--检测基本牌和任意锦囊
local isbasicortrick =function (card)
    return (card.type == Card.TypeBasic or card:isCommonTrick()) and not card.is_derived 
end

local jinwei = fk.CreateTriggerSkill{
    name = "xiaobai__jinwei",
    anim_type = "defensive",
    events = {fk.CardUsing},
    can_trigger = function(self, event, target, player, data)
      return player:hasSkill(self) 

       and (data.card.name == "jink" or (player:getMark("@@jinwei-turn")>0 and isbasicortrick(data.card)))
    end,
    on_cost = function(self, event, target, player, data)
      return player.room:askForSkillInvoke(player, self.name, data)
    end,
    on_use = function(self, event, target, player, data)
      player:turnOver()
      data.toCard = nil
      data.tos = {}
      if player.faceup then
        player.room:setPlayerMark(player,"@@jinwei-turn",1)
      end
    end,
}
--[[
    [":xiaobai__jinwei"] = [[每轮限X次（X为势力数），一名角色使用【闪】时，你可以将武将牌翻面，然后无效之。若你的武将牌因此翻至正面，
    本回合你可以以此法无效任意基本牌或普通锦囊牌。]]
--]]
  
  liuying:addSkill(jinwei)
  
  Fk:loadTranslationTable{
    ["xiaobai-ten__liuying"]="柳隐",
    ["xiaobai__jinwei"] = "金围",
    [":xiaobai__jinwei"] = [[一名角色使用【闪】时，你可以将武将牌翻面，然后无效之。若你的武将牌因此翻至正面，
    本回合你可以以此法无效任意基本牌或普通锦囊牌。]],
    ["@@jinwei-turn"]="金围 已启动",
    ["illustrator:xiaobai-ten__liuying"] = "YanBai",
    ["designer:xiaobai-ten__liuying"] = "zzcclll朱苦力",
    ["#xiaobai-ten__liuying"] = "孤原皲木",

  }


--[[
你攻击范围内其他角色使用【闪】时
你可以将武将牌翻面，然后无效之，若你
的武将牌因此翻至正面，本回合你可以以
此法无效任意基本牌或普通锦囊牌。
--]]


local liuji = General(extension, "xiaobai-ten__liuji", "wu", 3)--刘基



local meijian= fk.CreateTriggerSkill{
  name = "xiaobai__meijian",
  anim_type = "masochism",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self.name) then return false end

    if player:usedSkillTimes(self.name, Player.HistoryRound) > 0 then return false end
    local players=player.room:getOtherPlayers(player)
    for _, ps in ipairs(players) do
      local isget=false
      for _, move in ipairs(data) do
        if move.to == ps.id and move.toArea == Card.PlayerHand then
          isget=true
        end
        for _, info in ipairs(move.moveInfo) do
          if move.from == ps.id and info.fromArea == Card.PlayerHand then
            isget=true
          end
        end
      end
      if not isget then return end
      if table.every(player.room.alive_players, function (p) return p:getHandcardNum() >= ps:getHandcardNum() end) then
        self.cost_data={ps.id,2}--手牌最小
        return true
      elseif table.every(player.room.alive_players, function (p) return p:getHandcardNum() <= ps:getHandcardNum() end) then
        self.cost_data={ps.id,1}--手牌最大
        return true
      end

    end

  end,
 
  on_use = function(self, event, target, player, data)
    local room = player.room
    local ta=room:getPlayerById(self.cost_data[1])

    local pls={player,ta}
    local type=self.cost_data[2]
    local getplayernum=function (room,type)--获取手牌最少的角色数
    local x=0
    local num=0
      if type==1 then
        	-- 筛选最少手牌
          num=999
	      for _,p in ipairs(room.alive_players) do
		      if p:getHandcardNum() <=num then
            num= p:getHandcardNum()
		      end
	      end
	   
      elseif type==2 then --获取手牌最多的角色数
        for _,p in ipairs(room.alive_players) do
		      if   p:getHandcardNum() >=num then
            num= p:getHandcardNum()
		      end
	      end
      end
      for _,p in ipairs(room.alive_players) do
        if  p:getHandcardNum() ==num then
          x=x+1
        end
      end
      return x
      
    end
    local s=getplayernum(room,type)
    local cardnum={}
    local pcard={}
    for i,p in ipairs(pls) do
      local n = #p.player_cards[Player.Hand] - s
      cardnum[i]=n
      if n < 0 then
        pcard[i]=player:drawCards(-n, self.name)
      else
        pcard[i]= room:askForDiscard(p, n, n, false, self.name, false, ".", "#meijian-discard:::"..n)
      end

    end
    if math.abs(cardnum[2])> math.abs(cardnum[1]) then
      if room:askForSkillInvoke(ta, self.name, nil, "#meijian-askget") then
        room:obtainCard(ta, pcard[1], false,fk.ReasonPrey)
      end
    end
    
  end,
}
liuji:addSkill(meijian)

local yongchong = fk.CreateTriggerSkill{
  name = "xiaobai__yongchong",
  anim_type = "drawcard",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self.name) then return false end
    if player:usedSkillTimes(self.name, Player.HistoryTurn) > 0 then return false end
    local iflose=false
    local color

    for _, move in ipairs(data) do
      if move.from == player.id then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
            color= Fk:getCardById(info.cardId).color or 3
            iflose=true
          end
        end
      end
    end
    if not iflose then return end--判断是否失去牌
    --检验牌的颜色
    local room = player.room
    local check=true
    room.logic:getEventsOfScope(GameEvent.MoveCards, 99, function (e)
    for _, move in ipairs(e.data) do
      if move.from == player.id  then
        for _, info in ipairs(move.moveInfo) do
            if Fk:getCardById(info.cardId).color~=color then
              check=false
            end
        end
      end
    end
    end, Player.HistoryRound) 
    return check
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(2, self.name, nil, "@@yongchong-inhand")
    local lose=false
    for _, move in ipairs(data) do
      if move.from == player.id then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
            if Fk:getCardById(info.cardId):getMark("@@yongchong-inhand") > 0  then lose=true end
          end
        end
      end
    end
    if lose then
      room:handleAddLoseSkills(player, "-xiaobai__yongchong", nil, true, false)

    end
  end,

}

liuji:addSkill(yongchong)






Fk:loadTranslationTable{
  ["xiaobai-ten__liuji"] = "刘基",
  ["#xiaobai-ten__liuji"] = "完珙以瓊",
  ["illustrator:xiaobai-ten__liuji"] = "福尔马林射手",
  ["designer:xiaobai-ten__liuji"] = "廷玉",
  ["xiaobai__meijian"]="美谏",
  [":xiaobai__meijian"]=[[每轮限一次，当一名其他角色获得或失去手牌后，若其成为全场手牌数最大或最小的角色，你可以与其依次将手牌数调整至
满足另一者的角色数，若你调整的牌数小于其，其可以获得你因此弃置或摸的牌。]],
["#meijian-discard"]="美谏：弃置%arg张手牌",
["#meijian-askget"]="你可以获得刘基因此弃置或摸的牌。",
["@@yongchong-inhand"]="用宠",

["xiaobai__yongchong"]="用宠",
[":xiaobai__yongchong"]="每回合限一次，当你失去牌后，若本轮你只失去过该颜色的牌，你可以摸两张牌;若你因失去以此法获得的牌而发动【用宠】，你失去此技能。",

}




--[[
每轮限一次，当一名角色成为全场手牌数最大或
最小的角色后，你可以与其依次将手牌数调整至
满足另一者的角色数，若你调整的牌数小干其，
其可以获得你因此弃置或摸的牌。
每回合限一次，当你失去牌后，若本轮你只失去
过该颜色的牌，你可以摸两张牌;若你因失去以
此法获得的牌而发动“用宠”，你失去此技能
--]]


local zhushixing = General(extension, "xiaobai-ten__zhushixing", "wei", 3)
local qiudao = fk.CreateTriggerSkill{
  name = "xiaobai__qiudao",
  anim_type = "drawcard",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and
      #player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
        return table.contains({1, 11, 12, 13}, e.data[1].card.number)
      end, Player.HistoryTurn) == 0
  end,
  on_cost = function (self, event, target, player, data)
    local choice = player.room:askForChoice(player, {"1", "2", "3", "4", "5", "Cancel"}, self.name, "#xiaobai__qiudao-choice")
    if choice ~= "Cancel" then
      self.cost_data = {choice = tonumber(choice)}
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = table.simpleClone(room:getNCards(self.cost_data.choice))
    local use =  U.askForUseRealCard(room, player, {cards[#cards]}, nil, self.name, "#xiaobai__qiudao-use",
      {
        bypass_times = true,
        extraUse = true,
        expand_pile = cards,
      }, true, true)
    if use then
      table.remove(cards, #cards)
    else
      return
    end
    room:useCard(use)
    if player.dead or not table.contains({1, 11, 12, 13}, use.card.number) then return end
    cards = table.filter(cards, function (id)
      return table.contains(room.draw_pile, id)
    end)
    if #cards == 0 then return end
    room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, self.name, nil, false, player.id)
    cards = table.filter(cards, function (id)
      return table.contains(player:getCardIds("h"), id)
    end)
    if #cards == 0 or player.dead then return end
    local result = room:askForGuanxing(player, cards, nil, nil, self.name, true, {"Bottom", "toObtain"})
    if #result.top > 0 then
      room:moveCards{
        ids = result.top,
        from = player.id,
        toArea = Card.DrawPile,
        moveReason = fk.ReasonJustMove,
        skillName = self.name,
        drawPilePosition = -1,
        moveVisible = false,
      }
    end
  end,
}
local chuanjing = fk.CreateTriggerSkill{
  name = "xiaobai__chuanjing",
  anim_type = "control",
  events = {fk.DamageCaused, fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#xiaobai__chuanjing-invoke")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for i = 1, 3, 1 do
      local id = table.remove(room.draw_pile, #room.draw_pile)
      table.insert(room.draw_pile, 1, id)
    end
    room:syncDrawPile()
    local targets = {player.id}
    local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn)
    if turn_event and not turn_event.data[1].dead then
      table.insertIfNeed(targets, turn_event.data[1].id)
    end
    local to = room:askForChoosePlayers(player, targets, 1, 1, "#xiaobai__chuanjing-choose", self.name, true)
    if #to > 0 then
      to = room:getPlayerById(to[1])
      U.swapCardsWithPile(to, to:getCardIds("h"), room:getNCards(3), self.name, "Top")
    end
  end,
}
AddWinAudio(zhushixing)
zhushixing:addSkill(qiudao)
zhushixing:addSkill(chuanjing)
Fk:loadTranslationTable{
  ["xiaobai-ten__zhushixing"] = "朱士行",
  ["#xiaobai-ten__zhushixing"] = "西行求法",
  ["illustrator:xiaobai-ten__zhushixing"] = "木美人",
  ["designer:xiaobai-ten__zhushixing"] = "虎鲸",
  ["cv:xiaobai-ten__zhushixing"] = "康了个猫",

  ["xiaobai__qiudao"] = "求道",
  [":xiaobai__qiudao"] = "没有字母牌被使用过的回合结束时，你可以观看牌堆顶至多五张牌，然后使用其中最后一张牌，若你以此法使用了字母牌，则你获得"..
  "其余牌，并可将其中任意张置于牌堆底。",
  ["xiaobai__chuanjing"] = "传经",
  [":xiaobai__chuanjing"] = "当你造成或受到伤害时，你可以将牌堆底的三张牌置于牌堆顶，然后可以令你或当前回合角色用所有手牌交换牌堆顶的三张牌。",
  ["#xiaobai__qiudao-choice"] = "求道：你可以观看牌堆顶至多五张牌，使用最后一张牌",
  ["#xiaobai__qiudao-use"] = "求道：你可以使用最后一张牌",
  ["#xiaobai__chuanjing-invoke"] = "传经：是否将牌堆底三张牌置于牌堆顶？",
  ["#xiaobai__chuanjing-choose"] = "传经：你可以令你或当前回合角色用所有手牌交换牌堆顶三张牌",

  ["$xiaobai__qiudao1"] = "见心明性，念念回首，即是灵山。",
  ["$xiaobai__qiudao2"] = "法本心生，还从心灭，生灭由谁？",
  ["$xiaobai__chuanjing1"] = "上弘佛道，下化众生，断见思惑。",
  ["$xiaobai__chuanjing2"] = "远历千山寻经偈，善缘不灭终能抵。",
  ["~xiaobai-ten__zhushixing"] = "孤云将野鹤，岂向人间住？",
  ["$xiaobai-ten__zhushixing_win_audio"] = "无挂碍、无利害，不管世事兴与败。",
}

local liumin = General(extension, "xiaobai-ten__liumin", "shu", 3)
--印影（江山如故版）
local getShade = function (room, n)
  local ids = {}
  for _, id in ipairs(room.void) do
    if n <= 0 then break end
    if Fk:getCardById(id).name == "shade" then
      room:setCardMark(Fk:getCardById(id), MarkEnum.DestructIntoDiscard, 1)
      table.insert(ids, id)
      n = n - 1
    end
  end
  while n > 0 do
    local card = room:printCard("shade", Card.Spade, 1)
    room:setCardMark(card, MarkEnum.DestructIntoDiscard, 1)
    table.insert(ids, card.id)
    n = n - 1
  end
  return ids
end
local lianzhi = fk.CreateTriggerSkill{
  name = "xiaobai__lianzhi",
  anim_type = "drawcard",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and player:isKongcheng() then
      for _, move in ipairs(data) do
        if move.from == player.id then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#xiaobai__lianzhi1-invoke")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = {}
    if Fk.all_card_types["shade"] then
      cards = getShade(room, 3)
    elseif Fk.all_card_types["rfenghou__shade"] then
      cards = RUtil.getShade(room, 3)
    end
    assert(#cards > 0, "服务器未加入【影】！请联系管理员安装“江山如故”或“封侯”包")
    room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, self.name, nil, true, player.id)
  end,
}
local lianzhi_trigger = fk.CreateTriggerSkill{
  name = "#xiaobai__lianzhi_trigger",
  anim_type = "drawcard",
  main_skill = lianzhi,
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(lianzhi) and not table.find(player:getCardIds("h"), function (id)
      return Fk:getCardById(id).trueName == "shade"
    end) then
      for _, move in ipairs(data) do
        if move.from == player.id then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand and Fk:getCardById(info.cardId).trueName == "shade" then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#xiaobai__lianzhi2-invoke")
  end,
  on_use = function(self, event, target, player, data)
    if not player:isKongcheng() then
      player:showCards(player:getCardIds("h"))
    end
    if not player.dead then
      player:drawCards(3, self.name)
    end
  end,
}
--[[local yizuo = fk.CreateActiveSkill{
  name = "xiaobai__yizuo",
  anim_type = "support",
  card_num = 0,
  target_num = 1,
  prompt = "#xiaobai__yizuo",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and not player:isKongcheng()
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= Self.id
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local extra_data = {
      num = 1,
      min_num = 1,
      include_equip = false,
      skillName = self.name,
      pattern = ".",
    }
    player.request_data = json.encode({ "choose_cards_skill", "#xiaobai__yizuo1-ask:"..target.id, false, extra_data })
    local extra_data2 = {
      num = 1,
      min_num = 1,
      include_equip = false,
      skillName = self.name,
      pattern = tostring(Exppattern{ id = player:getCardIds("h") }),
      expand_pile = player:getCardIds("h"),
    }
    target.request_data = json.encode({ "choose_cards_skill", "#xiaobai__yizuo2-ask:"..player.id, false, extra_data2 })
    room:notifyMoveFocus({player, target}, self.name)
    room:doBroadcastRequest("AskForUseActiveSkill", {player, target})

    local cards = {}
    for _, p in ipairs({player, target}) do
      local id = nil
      if p.reply_ready then
        local replyCard = json.decode(p.client_reply).card
        id = json.decode(replyCard).subcards[1]
      end
      if id == nil then
        id = table.random(player:getCardIds("h"))  --for AI
      end
      table.insertIfNeed(cards, id)
      room:addTableMark(Fk:getCardById(id), "@xiaobai__yizuo", Fk:translate(p.general))
    end

    room:setPlayerMark(player, "xiaobai__yizuo-tmp", {{player.id, target.id}, cards})
    local success, dat = room:askForUseActiveSkill(player, "xiaobai__yizuo_active",
      "#xiaobai__yizuo-ask::"..target.id, true)
    room:setPlayerMark(player, "xiaobai__yizuo-tmp", 0)
    for _, id in ipairs(cards) do
      room:setCardMark(Fk:getCardById(id), "@xiaobai__yizuo", 0)
    end
    if success and dat then
      room:useVirtualCard(dat.interaction, cards, player, table.map(dat.targets, Util.Id2PlayerMapper), self.name)
    end
  end,
}]]--
local yizuo = fk.CreateTriggerSkill{
  name = "xiaobai__yizuo",
  anim_type = "support",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target.phase == Player.Play and not target.dead and not player:isKongcheng() and
      (target ~= player or #player:getTableMark("@xiaobai__yizuo-round") < 4)
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askForSkillInvoke(player, self.name, nil, "#xiaobai__yizuo-invoke::"..target.id) then
      self.cost_data = {tos = {target.id}}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choices = {}
    for _, id in ipairs(player:getCardIds("h")) do
      local suit = Fk:getCardById(id):getSuitString(true)
      if suit ~= "log_nosuit" and not table.contains(player:getTableMark("@xiaobai__yizuo-round"), suit) then
        table.insertIfNeed(choices, suit)
      end
    end
    if #choices == 0 then
      if target == player then
        return
      else
        U.viewCards(target, player:getCardIds("h"), self.name, "#xiaobai__yizuo-choice:"..player.id)
        return
      end
    end
    local tos = {player}
    if target ~= player then
      tos = {player, target}
    end
    local req = Request:new(tos, "CustomDialog")
    req.focus_text = self.name
    local extraData = {
      player:getCardIds("h"),
      {},
      "#xiaobai__yizuo-choice:"..player.id,
      choices,
      1,
      1,
      player:getCardIds("h"),
    }
    for _, p in ipairs(tos) do
      req:setData(p, { path = "packages/utility/qml/ChooseCardsAndChoiceBox.qml", data = extraData, })
    end
    req:ask()
    local suits = {}
    for _, p in ipairs(tos) do
      local suit = choices[1]
      if req:getResult(p) ~= "" then
        suit = req:getResult(p).choice
      end
      table.insertIfNeed(suits, suit)
      room:sendLog{
        type = "#Choice",
        from = p.id,
        arg = suit,
        toast = true,
      }
      if not table.contains(player:getTableMark("@xiaobai__yizuo-round"), suit) then
        room:addTableMark(player, "@xiaobai__yizuo-round", suit)
      end
    end
    local cards = table.filter(player:getCardIds("h"), function (id)
      return table.contains(suits, Fk:getCardById(id):getSuitString(true))
    end)

    room:setPlayerMark(player, "xiaobai__yizuo-tmp", {table.map(tos, Util.IdMapper), cards})
    local success, dat = room:askForUseActiveSkill(player, "xiaobai__yizuo_active",
      "#xiaobai__yizuo-ask::"..target.id, true)
    room:setPlayerMark(player, "xiaobai__yizuo-tmp", 0)
    if success and dat then
      room:useVirtualCard(dat.interaction, cards, player, table.map(dat.targets, Util.Id2PlayerMapper), self.name)
    end
    if not player.dead and #suits > 1 then
      room:setPlayerMark(player, "@xiaobai__yizuo-round", 0)
      room:loseHp(player, 1, self.name)
    end
  end,
}
local yizuo_active = fk.CreateActiveSkill{
  name = "xiaobai__yizuo_active",
  target_num = 1,
  interaction = function(self)
    return UI.CardNameBox {choices = {"ex_nihilo", "peach"}}
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected)
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcards(Self:getMark("xiaobai__yizuo-tmp")[2])
    card.skillName = "xiaobai__yizuo"
    if #selected == 0 and table.contains(Self:getMark("xiaobai__yizuo-tmp")[1], to_select) then
      local target = Fk:currentRoom():getPlayerById(to_select)
      if Self:prohibitUse(card) or Self:isProhibited(target, card) then return false end
      return card.skill:modTargetFilter(target.id, {}, Self.id, card, false)
    end
  end,
}
Fk:addSkill(yizuo_active)
lianzhi:addRelatedSkill(lianzhi_trigger)
liumin:addSkill(yizuo)
liumin:addSkill(lianzhi)
Fk:loadTranslationTable{
  ["xiaobai-ten__liumin"] = "刘敏",
  ["#xiaobai-ten__liumin"] = "缜承渊虑",
  ["illustrator:xiaobai-ten__liumin"] = "nihilum",
  ["designer:xiaobai-ten__liumin"] = "王秀丽",

  ["xiaobai__yizuo"] = "翼佐",
  --[[[":xiaobai__yizuo"] = "出牌阶段限一次，你可以令一名其他角色观看你的手牌，你与其同时选择其中一张牌，然后你可以将被选择的所有牌当【无中生有】"..
  "或【桃】对你或其使用。",]]--
  [":xiaobai__yizuo"] = "一名角色的出牌阶段开始时，你可以令其观看你的手牌，你与其同时选择其中一种花色（本轮已选择过的除外），然后你可以将"..
  "被选择的花色牌当【无中生有】或【桃】对你或其使用；若你与其选择花色不同，你失去1点体力，移去本轮已被选择过的花色。",
  ["xiaobai__lianzhi"] = "连帜",
  [":xiaobai__lianzhi"] = "当你失去最后一张手牌后，你可以获得三张【影】；当你失去手牌中最后一张【影】后，你可以展示手牌（无牌则不展示），摸三张牌。",
  --["#xiaobai__yizuo"] = "翼佐：令一名角色观看你的手牌，与其同时选择其中一张牌，将这些牌当【无中生有】或【桃】对你或其使用",
  ["#xiaobai__yizuo-invoke"] = "翼佐：是否令 %dest 观看你的手牌，并与其同时选择一种花色？",
  ["#xiaobai__yizuo-choice"] = "翼佐：观看 %src 的手牌并选择一种花色",
  ["@xiaobai__yizuo-round"] = "翼佐",
  ["xiaobai__yizuo_active"] = "翼佐",
  ["#xiaobai__yizuo-ask"] = "翼佐：你可以将这些牌当【无中生有】或【桃】对你或 %dest 使用",
  ["#xiaobai__lianzhi1-invoke"] = "连帜：是否获得三张【影】？",
  ["#xiaobai__lianzhi2-invoke"] = "连帜：是否展示所有手牌并摸三张牌？",
  ["#xiaobai__lianzhi_trigger"] = "连帜（摸牌）",
}

local xiaobai__dinggu = General:new(extension, "xiaobai-ten__dinggu", "wu", 3)

local xiaobai_chenzhu = fk.CreateActiveSkill{
  name = "xiaobai_chenzhu",
  anim_type = "offensive",
  can_use = function (self, player, card, extra_data)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and #player:getCardIds("he")>0 
  end,
  target_num = 0,
  min_card_num = 1,
  max_card_num = 2,
  prompt = "#xiaobai_chenzhu-description",
  card_filter = function (self, to_select, selected, selected_targets)
    return #selected <  2
  end,
  target_filter = Util.FalseFunc,
  on_use = function (self, room, effect)
    local player = room:getPlayerById(effect.from)
    local cards1 = effect.cards
    room:recastCard(effect.cards,player,self.name)
    if player.dead then
      return
    end
    local target_all = table.map(table.filter(room:getOtherPlayers(player),function (element, index, array)
      return #element:getCardIds("he")>0
    end),Util.IdMapper)

    local targets
    if #target_all > 0 then
      targets = room:askForChoosePlayers(player,target_all,1,1,"#xiaobai_chenzhu-choose_other_player",self.name,true)
      local cards2 = {}
      if targets and #targets>0 then
        local target = room:getPlayerById(targets[1])
        cards2 = room:askForCard(target,1,2,true,self.name,false,".","#xiaobai_chenzhu-other_choose_cards",self.name,false)
        if #cards2 then
          room:recastCard(cards2,target,self.name)
        end
      end
    end


    if player.dead then return end
    local all_cards = {}
    for index, value in ipairs(cards1) do
      table.insert(all_cards,Fk:getCardById(value))
    end
    if cards2 ~= nil and #cards2 > 0 then
      for index, value in ipairs(cards2) do
        table.insert(all_cards,Fk:getCardById(value))
      end
    end
    if #all_cards <2 then return end
    local heart,diomand,club,spade = false,false,false,false
    for index, value in ipairs(all_cards) do
      if value.suit == Card.Heart then
        if heart then
          return 
        end
        heart = true
      elseif value.suit == Card.Club then
        if club then
          return 
        end
        club = true
      elseif value.suit == Card.Diamond then
        if diomand then
          return 
        end
        diomand = true
      elseif value.suit == Card.Spade then
        if spade then
          return 
        end
        spade = true
      end
    end
    --[[local slash = Fk:cloneCard("slash")
    if player:prohibitUse(slash) then return end
    local max_num = slash.skill:getMaxTargetNum(player, slash)
    local slash_targets = {}
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if not player:isProhibited(p, slash) and player:inMyAttackRange(p) then
        table.insert(slash_targets, p.id)
      end
    end
    if #slash_targets == 0 or max_num == 0 then return end
    local tos = room:askForChoosePlayers(player, slash_targets, 1, max_num, "#tsp_chenzhu-use_slash", self.name, true)
    if tos and #tos > 0 then
      room:useVirtualCard("slash", nil, player, table.map(tos, function(id) return room:getPlayerById(id) end), self.name, true)
      if not player.dead and #room:getPlayerById(tos[1]):getCardIds("ej")>0 then
        local move_target = room:askForChoosePlayers(player,
          table.map(room:getOtherPlayers(room:getPlayerById(tos[1])),Util.IdMapper),1,1,"#tsp_chenzhu-move_target::"..tos[1],self.name,true)
        if move_target then
          room:askForMoveCardInBoard(player,room:getPlayerById(tos[1]),room:getPlayerById(move_target[1]),self.name,nil,room:getPlayerById(tos[1]))
        end
      end
    end]]
    local use = player.room:askForUseCard(player, "slash", "slash", "#xiaobai_chenzhu-use_slash", true,{bypass_times = true})
    if use then
      player.room:useCard(use)
      local tos = use.tos[1]
    end
  end
}

local xiaobai_lvjie = fk.CreateTriggerSkill{
  name = "xiaobai_lvjie",
  events = {fk.Damage,fk.AfterCardsMove},
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player,self.name,nil,"#xiaobai_lvjie-use")
  end,
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(self) then
      return false
    end
    if event == fk.Damage and player:getMark("lvjie_damage-round") == 0 then
      return target == player
    else
      if player:getMark("lvjie_cardMove-round") > 0 then
        return false
      end
      for _, move in ipairs(data) do
        if move.to and move.to == player.id and move.toArea == Player.Equip then
          return true
        end
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    player.room:setPlayerMark(player,"xiaobai_lvjie-using",1)
    player.room:useVirtualCard("bogus_flower",nil,player,{player},self.name)
    player.room:setPlayerMark(player,"xiaobai_lvjie-using",0)
    local history_num = 0
    if event == fk.Damage then
      history_num = player:getMark("@lvjie-maxCard1") or 0
      player.room:setPlayerMark(player,"lvjie_damage-round",1)
    else
      history_num = player:getMark("@lvjie-maxCard2") or 0
      player.room:setPlayerMark(player,"lvjie_cardMove-round",1)
    end
    if not player.dead then
      local card_num = player:getMark("xiaobai_lvjie_cardnum")
      print(card_num)
      
      if card_num > history_num then
        player.room:drawCards(player,1,self.name)
        if event == fk.Damage then
          player.room:setPlayerMark(player,"@lvjie-maxCard1",card_num)
        else
          player.room:setPlayerMark(player,"@lvjie-maxCard2",card_num)
        end
      end
      local can_move = player:getMark("@lvjie-maxCard1") == 3 and player:getMark("@lvjie-maxCard2") == 3
      if can_move then
        local targets = player.room:askForChooseToMoveCardInBoard(player, "#xiaobai_lvjie-move", self.name, true, nil)
        if #targets ~= 0 then
          targets = table.map(targets, function(id) return player.room:getPlayerById(id) end)
          player.room:askForMoveCardInBoard(player, targets[1], targets[2], self.name)
        end
      end
    end
    player.room:setPlayerMark(player,"xiaobai_lvjie_cardnum",0)
  end,
  refresh_events = {fk.AfterCardsMove},
  can_refresh = function (self, event, target, player, data)
    if (not player:hasSkill(self)) or (player:getMark("xiaobai_lvjie-using") == nil or player:getMark("xiaobai_lvjie-using") == 0)  then
      return false
    end
    self.cost_data = {}
    for _, move in ipairs(data) do
      if move.to == player.id and move.toArea == player.Hand and move.skillName == "bogus_flower" and move.proposer == player.id then
        for _, info in ipairs(move.moveInfo) do
            table.insertIfNeed(self.cost_data, info.cardId)
        end
      end
    end
    if #self.cost_data > 0 then
      return true
    end
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player,"xiaobai_lvjie_cardnum",#self.cost_data)
  end
}

xiaobai__dinggu:addSkill(xiaobai_chenzhu)
xiaobai__dinggu:addSkill(xiaobai_lvjie)

Fk:loadTranslationTable {
  ["xiaobai-ten__dinggu"] = "丁固",
  ["#xiaobai-ten__dinggu"] = "烈胆匡国",
  ["illustrator:xiaobai-ten__dinggu"] = "鸿图之下",
  ["designer:xiaobai-ten__dinggu"] = "qqqqq",
  ["cv:xiaobai-ten__dinggu"] = "爬不上树的猴子",
  ["xiaobai_chenzhu"] = "沉诛",
  [":xiaobai_chenzhu"] = "出牌阶段限一次，你可以重铸至多两张牌，然后你令一名有牌的其他角色也可如此做(没人有牌则跳过)，"..
                     "若你与其被重铸的牌花色均不同，则你可以视为使用一张【杀】。",
  ["#xiaobai_chenzhu-description"] = "发动 沉诛 ，重铸至多两张牌",
  ["#xiaobai_chenzhu-choose_other_player"] = "沉诛：选择一名其他角色，令其也可重铸至多两张牌",
  ["#xiaobai_chenzhu-other_choose_cards"] = "沉诛：你可以重铸至多两张牌",
  ["#xiaobai_chenzhu-use_slash"] = "沉诛：你可以使用一张【杀】",
  ["xiaobai_lvjie"] = "履阶",
  ["@lvjie-maxCard1"] = "履阶伤害",
  ["@lvjie-maxCard2"] = "履阶装备",
  ["#xiaobai_lvjie-use"] = "履阶：你可以视为使用一张【树上开花】",
  ["#xiaobai_lvjie-move"] = "履阶：你可以移动场上一张牌",
  [":xiaobai_lvjie"] = "每轮每项限一次，①你造成伤害后②装备牌置入你的装备区后，你可以视为使用一张【树上开花】，若你于此时机以此法获得的牌为此前唯一最多，你摸一张牌，"..
                   "然后若你发动过另一项且均以此【树上开花】摸过至少三张牌，你移动场上一张牌。",

  ["$xiaobai_chenzhu1"] = "职司不忠、奸党相扶,百姓愁劳，何以用治？",
  ["$xiaobai_chenzhu2"] = "王者受之于天，今上逆犯天地，吾等当断。",
  ["$xiaobai_lvjie1"] = "幸履丹墀，敢辞舟楫？犯波澜者真儒事。",
  ["$xiaobai_lvjie2"] = "紫薇高拱五云端，柯叶作槎可登揽。",
  ["~xiaobai-ten__dinggu"] = "江湖自笑、故旧谁怜？此阁从今长闭。"
}

return extension